package com.ikun.controller;
import com.ikun.pojo.Result;
import com.ikun.pojo.TbUser;
import com.ikun.service.UserService;
import com.ikun.util.UploadUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    UserService userService;

    //查询好人榜的方法
    @RequestMapping("selGoodman")
    @ResponseBody
    public List<TbUser> selGoodman(HttpServletRequest request, HttpServletResponse response){
        int date = Integer.parseInt(request.getParameter("date"));
        List<TbUser> tbUserList = userService.selGoodman(date);
        return tbUserList;
    }

    //查询用户方法
    @RequestMapping("selSessionUser")
    @ResponseBody
    public TbUser selSessionUser(HttpServletRequest request, HttpServletResponse response){

        TbUser tbUserNow = (TbUser) request.getSession().getAttribute("tbUserNow");
          return tbUserNow;
    }

    //用户登录方法
    @RequestMapping("userlong")
    public String userlong(HttpServletRequest request, HttpServletResponse response){
        String account = request.getParameter("account");
        String pwd = request.getParameter("pwd");
        TbUser tbUserNow = userService.getUser(account,pwd);
        if(tbUserNow!=null){
            request.getSession().setAttribute("tbUserNow",tbUserNow);
            return "index";
        }
        return "login";
    }

    //退出账号
    @RequestMapping("logoutAccount")
    public String logoutAccount(HttpServletRequest request, HttpServletResponse response){
        request.getSession().removeAttribute("tbUserNow");
        return "index";
    }
    //查询所有用户的方法
    @RequestMapping("showusers")
    @ResponseBody
    public Result showusers(HttpServletRequest request, HttpServletResponse response){
        Result result=new Result();
        List<TbUser> list = userService.showusers();
        if (list!=null) {
            result.setCode(0);
            result.setMessage("有数据");
            result.setData(list);
        }else {
            result.setCode(0);
            result.setMessage("无数据");
            result.setData(list);
        }
        return result;
    }
    //删除用户的方法
    @RequestMapping("deluser")
    @ResponseBody
    public int deluser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int userid = Integer.parseInt(request.getParameter("id"));
        int count = userService.deluser(userid);
        System.out.println(count);
        return count;
    }

    //修改用户头像的方法
    @RequestMapping("updateMyPic")
    @ResponseBody
    public Result updateMyPic(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String url = UploadUtil.uploadPic(request,response);
        Result result = new Result();
        if (null != url){
            TbUser tbUserNow = (TbUser)request.getSession().getAttribute("tbUserNow");
            Long id = tbUserNow.getId();
            int count = userService.updateMyPic(id,url);
            if(count > 0){
                tbUserNow.setPic(url);
                request.getSession().setAttribute("tbUserNow",tbUserNow);
                result.setMessage(url);
            }else{
                result.setMessage("0");
            }
        }else{
            result.setMessage("0");
        }
        return result;
    }

    //修改用户密码
    @RequestMapping("updateMyPwd")
    @ResponseBody
    public Result updateMyPwd(HttpServletRequest request, HttpServletResponse response) throws Exception {
        TbUser tbUserNow = (TbUser)request.getSession().getAttribute("tbUserNow");
        Long id = tbUserNow.getId();
        String oldPwd = request.getParameter("oldpwd");
        String newPwd = request.getParameter("newpwd");
        System.out.println(id+" "+oldPwd+" "+newPwd);
        int count = userService.updateMyPwd(id,oldPwd,newPwd);
        Result result = new Result();
        if (count > 0){
            tbUserNow.setPwd(newPwd);
            request.getSession().setAttribute("tbUserNow",tbUserNow);
            result.setMessage("1");
        }else{
            result.setMessage("0");
        }
        return result;
    }

    //修改用户信息
    @RequestMapping("updateMyInfo")
    @ResponseBody
    public Result updateMyInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
        TbUser tbUserNow = (TbUser)request.getSession().getAttribute("tbUserNow");
        Long id = tbUserNow.getId();
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String nickname = request.getParameter("nickname");
        String sex = request.getParameter("sex");
        String department = request.getParameter("department");
        String profession = request.getParameter("profession");
        String classes = request.getParameter("classes");
        String realname = request.getParameter("realname");
        TbUser tbUser = new TbUser(id,nickname,null,realname,null,null,sex,email,phone,department,profession,classes,new BigDecimal("0"),true,true,null,0,0);
        System.out.println(phone+" "+email+" "+nickname+" "+sex+" "+department+" "+profession+" "+classes+" "+realname);
        int count = userService.updateMyInfo(tbUser);
        Result result = new Result();
        if (count > 0){
            tbUserNow.setPhone(phone);
            tbUserNow.setEmail(email);
            tbUserNow.setNickname(nickname);
            tbUserNow.setSex(sex);
            tbUserNow.setDepartment(department);
            tbUserNow.setProfession(profession);
            tbUserNow.setClasses(classes);
            tbUserNow.setRealname(realname);
            request.getSession().setAttribute("tbUserNow",tbUserNow);
            result.setMessage("1");
        }else{
            result.setMessage("0");
        }
        return result;
    }
    //扣款
    //如果是兑换的扣款就别调用了,我在UserPrizeController中的兑换接口中已经调用了
    @RequestMapping("deductmoney")
    @ResponseBody
    public Result deductMoney(Long userId, BigDecimal money){
        int count = userService.deductMoney(userId,money);
        Result result = new Result();
        result.setCode(1);
        if (count>0)result.setCode(0);
        return result;
    }

    //注册账号
    @RequestMapping("registeuUser")
    @ResponseBody
    public Result registerUser(String nickname,String account,String pwd,String email,String sex){
        int count = userService.registerUser(nickname,account,pwd,email,sex);
        Result result = new Result();
        result.setCode(1);
        if (count>0)result.setCode(0);
        return result;
    }
    //修改用户信息(管理端)
    @RequestMapping("updatemsg")
    @ResponseBody
    public Result updatemsg(HttpServletRequest request, HttpServletResponse response) throws Exception {

        int id = Integer.parseInt(request.getParameter("id"));
        String nickname = request.getParameter("nickname");
        String realname=request.getParameter("realname");
        String pwd=request.getParameter("pwd");
        String sex = request.getParameter("sex");
        String email=request.getParameter("email");
        String phone=request.getParameter("phone");
        String department = request.getParameter("department");
        String profession = request.getParameter("profession");
        String classes = request.getParameter("classes");
        int count = userService.updatemsg(id,nickname,realname,pwd,sex,email,phone,department,profession,classes);
        Result result = new Result();
        if (count > 0){
            result.setCode(0);
            result.setMessage("修改成功");
        }else{
            result.setMessage("0");
        }
        return result;
    }
}
